<template>
  <div class="login-setting">
    <el-form ref="userForm" :rules="rules" :model="userInfo" label-width="80px" style="margin-left: 120px; margin-top: 30px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="userInfo.username" style="width:30%" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="userInfo.password" style="width:30%" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">确认更新</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getUserPhotoById } from '@/api/user'
import { saveUserInfo } from '@/api/employees'
export default {
  name: 'LoginSetting',
  data() {
    return {
      userInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '员工姓名不能为空', trigger: 'blur' },
          { min: 1, max: 5, message: '用户姓名为1-4位', trigger: 'blur' }],
        password: [{ required: true, message: '员工密码不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    // 调用获取信息的方法
    this.getUserPhotoById()
  },
  methods: {
    // 设置一个获取用户信息的API
    async getUserPhotoById() {
      const res = await getUserPhotoById(this.$route.query.id)
      this.userInfo = res.data
    },
    onSubmit() {
      this.$refs.userForm.validate(async valid => {
        if (!valid) return
        const res = await saveUserInfo(this.$route.query.id, this.userInfo)
        this.$message.success(res.message)
        this.getUserPhotoById()
      })
    }
  }
}
</script>
<style scoped lang="less"></style>
